﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using chesslibrary.cs;
using System.Media;
using System.Collections;

namespace ChessGui
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            thegame=new game_chess(new Manualplayer(color_pieces.White),new Manualplayer(color_pieces.Black));
			buttons=new Fields();
			images=new image_chess();
            Init();
            thegame.StartGame+= new ChessSimpleEvent(StartTheGame);
            thegame.EndGame+=new ChessSimpleEvent(EndTheGame);
            thegame.MoveUndone+=new ChessSimpleEvent(UndoTheMove);
            thegame.MoveExecuted+=new ChessMoveEvent(MakeMove);
		    thegame.Start();  
        }
        private void button1_Click(object sender, EventArgs e)
        {
            newgame();     
        }
        private void newgame()
        {
            thegame.Start();
        }
        private void Init()
        {
            foreach (ChessButton b in buttons)
            {
                label1.Text = "A";
                    label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label1.ForeColor = System.Drawing.Color.Yellow;
                label2.Text = "B";
                    label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label2.ForeColor = System.Drawing.Color.Yellow;
                label3.Text = "C";
                    label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label3.ForeColor = System.Drawing.Color.Yellow;
                label4.Text = "D";
                    label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label4.ForeColor = System.Drawing.Color.Yellow;
                label5.Text = "E";
                    label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label5.ForeColor = System.Drawing.Color.Yellow;
                label6.Text = "F";
                    label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label6.ForeColor = System.Drawing.Color.Yellow;
                label7.Text = "G";
                    label7.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label7.ForeColor = System.Drawing.Color.Yellow;
                label8.Text = "H";
                    label8.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label8.ForeColor = System.Drawing.Color.Yellow;
                label9.Text = "H";
                    label9.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label9.ForeColor = System.Drawing.Color.Yellow;
                label10.Text = "G";
                    label10.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label10.ForeColor = System.Drawing.Color.Yellow;
                label11.Text = "F";
                    label11.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label11.ForeColor = System.Drawing.Color.Yellow;
                label12.Text = "E";
                    label12.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label12.ForeColor = System.Drawing.Color.Yellow;
                label13.Text = "D";
                    label13.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label13.ForeColor = System.Drawing.Color.Yellow;
                label14.Text = "C";
                    label14.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label14.ForeColor = System.Drawing.Color.Yellow;
                label15.Text = "B";
                    label15.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label15.ForeColor = System.Drawing.Color.Yellow;
                label16.Text = "A";
                    label16.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label16.ForeColor = System.Drawing.Color.Yellow;
                label17.Text = "1";
                    label17.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label17.ForeColor = System.Drawing.Color.Yellow;
                label18.Text = "2";
                    label18.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label18.ForeColor = System.Drawing.Color.Yellow;
                label19.Text = "3";
                    label19.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label19.ForeColor = System.Drawing.Color.Yellow;
                label20.Text = "4";
                    label20.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label20.ForeColor = System.Drawing.Color.Yellow;
                label21.Text = "5";
                    label21.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label21.ForeColor = System.Drawing.Color.Yellow;
                label22.Text = "6";
                    label22.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label22.ForeColor = System.Drawing.Color.Yellow;
                label23.Text = "7";
                    label23.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label23.ForeColor = System.Drawing.Color.Yellow;
                label24.Text = "8";
                    label24.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label24.ForeColor = System.Drawing.Color.Yellow;
                label25.Text = "8";
                    label25.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label25.ForeColor = System.Drawing.Color.Yellow;
                label26.Text = "7";
                    label26.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label26.ForeColor = System.Drawing.Color.Yellow;
                label27.Text = "6";
                    label27.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label27.ForeColor = System.Drawing.Color.Yellow;
                label28.Text = "5";
                    label28.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label28.ForeColor = System.Drawing.Color.Yellow;
                label29.Text = "4";
                    label29.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label29.ForeColor = System.Drawing.Color.Yellow;
                label30.Text = "3";
                    label30.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label30.ForeColor = System.Drawing.Color.Yellow;
                label31.Text = "2";
                    label31.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label31.ForeColor = System.Drawing.Color.Yellow;
                label32.Text = "1";
                    label32.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label32.ForeColor = System.Drawing.Color.Yellow;
                why.Controls.Add(b);
            }
            buttons.onSelect += new ChessButtonHandler(buttonselect);
            buttons.onMove += new ChessButtonHandler(fieldmove);
            hinh.BackgroundImage = System.Drawing.Image.FromFile("thamtu.jpg");
            hinh2.BackgroundImage = System.Drawing.Image.FromFile("murom.jpg");
        }
        private void Init1()
        {
            foreach (ChessButton c in buttons)
            {
               why.Controls.Add(c);
            }
            buttons.onSelect += new ChessButtonHandler(buttonselect);
            buttons.onMove += new ChessButtonHandler(fieldmove);
            hinh.BackgroundImage = System.Drawing.Image.FromFile("thamtu.jpg");
            hinh2.BackgroundImage = System.Drawing.Image.FromFile("murom.jpg");
        }
       	private Fields buttons;
		private cell_chess selectedcell;
		private game_chess thegame;
        private image_chess images;	
		private void StartTheGame()
		{
			RefreshScreen();
		}
        private void EndTheGame()
		{
			RefreshScreen();
		}
        private void UndoTheMove()
		{
			RefreshScreen();
		}
        private void MakeMove(move_pieces m)
		{
			RefreshScreen();
		}
        protected void buttonselect(cell_chess cell)
        {
            selectedcell = cell;
            ClearScreen();
            ArrayList l = thegame.GetValidMoves(cell);
            IEnumerator i = l.GetEnumerator();
            while (i.MoveNext())
            {
                properties_cell test = (properties_cell)i.Current;
                buttons[test].BackColor = Color.Yellow;
            }
            buttons[selectedcell].BackColor = Color.DarkRed;
        }
        protected void fieldmove(cell_chess cell)
        {
            try
            {
                thegame.movePiece(selectedcell, cell);
                ClearScreen();
            }
            catch (ChessException e)
            {
                MessageBox.Show(e.ToString() + "\n at row " + e.Row + " and column " + e.Col);
            }
        }
        private void RefreshScreen()
		{
			foreach (ChessButton b in buttons)
			{
			pieces_chess p=thegame[b.Cell].pieces_chess;
					
				if (p==null)
				{
					b.Image=null;
				}
				else
				{
					if (p.IsOwnedBy(color_pieces.White))
					{
						b.ForeColor=Color.White;
					}
					else
					{
						b.ForeColor=Color.Black;
					}
					
					b.Image=images[p];
				}
			}
		}
        private void ClearScreen()
		{
			foreach (ChessButton b in buttons)
			{
				if (b.Cell.IsDark)
				{
                    b.BackColor = Color.Blue;
                }
				else
				{
                    b.BackColor = Color.DarkGreen;
				}
			}
		}
        private void button5_Click(object sender, EventArgs e)
        {
            thegame.undo();
        }
        private void button6_Click(object sender, EventArgs e)
        {
            Form f1 = new thong_tin();
            f1.Show();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void button4_Click(object sender, EventArgs e)
        {
            Help.ShowHelp(this, Application.StartupPath + "\\LuatCoVua.chm", HelpNavigator.TableOfContents);
        }      
    }
}
